package com.dwxt.cdms.admin.service;


import com.dwxt.cdms.base.OperateCategory;
import com.dwxt.cdms.dao.MenuMapper;
import com.dwxt.cdms.dao.RoleMenuMapper;
import com.dwxt.cdms.entity.Menu;
import com.dwxt.cdms.entity.MenuExample;
import com.dwxt.cdms.entity.RoleMenu;
import com.dwxt.cdms.entity.RoleMenuExample;
import com.dwxt.cdms.entity.vo.MenuVO;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceAdmin {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private HistoryServiceAdmin historyServiceAdmin;

    public void createOrUpdateMenu(Menu menu) {
        if (menu.getId() == null) {
            menuMapper.insertSelective(menu);
            //插入操作记录
            historyServiceAdmin.generate("新增菜单:" + menu.getName(), OperateCategory.INSERT);
        } else {
            menuMapper.updateByPrimaryKeySelective(menu);
            //插入操作记录
            historyServiceAdmin.generate("修改菜单:" + menu.getId(), OperateCategory.UPDATE);
        }
    }

    public Menu selectByName(String name) {
        MenuExample example = new MenuExample();
        example.createCriteria().andNameEqualTo(name);
        List<Menu> menus = menuMapper.selectByExample(example);
        return menus.isEmpty() ? null : menus.get(0);
    }

    public int batchDeleteById(Integer[] id) {
        MenuExample example = new MenuExample();
        example.createCriteria().andIdIn(Arrays.asList(id));
        int i = menuMapper.deleteByExample(example);
        //插入操作记录
        StringBuilder sb = new StringBuilder();
        for (Integer integer : id) {
            sb.append(integer).append(",");
        }
        historyServiceAdmin.generate("删除菜单:" + sb, OperateCategory.DELETE);
        return i;
    }

    public List<Menu> selectAll(Integer parentId) {
        MenuExample example = new MenuExample();
        example.setOrderByClause("create_time desc");
        if (parentId != null) {
            example.createCriteria().andParentIdEqualTo(parentId);
        }
        return menuMapper.selectByExample(example);
    }

    public List<MenuVO> selectAllTopMenu() {
        MenuExample example = new MenuExample();
        example.createCriteria().andParentIdEqualTo(0);
        List<Menu> menus = menuMapper.selectByExample(example);

        List<MenuVO> menuVOS = JsonUtils.jsonToList(JsonUtils.objectToJson(menus), MenuVO.class);
        for (MenuVO menuVO : menuVOS) {
            example.clear();
            example.setOrderByClause("create_time");
            example.createCriteria().andParentIdEqualTo(menuVO.getId());
            List<Menu> childMenus = menuMapper.selectByExample(example);

            List<MenuVO> menuVOList = JsonUtils.jsonToList(JsonUtils.objectToJson(childMenus), MenuVO.class);
            for (MenuVO vo : menuVOList) {
                example.clear();
                example.setOrderByClause("create_time");
                example.createCriteria().andParentIdEqualTo(vo.getId());
                List<Menu> childMenuList = menuMapper.selectByExample(example);
                vo.setChildMenus(JsonUtils.jsonToList(JsonUtils.objectToJson(childMenuList), MenuVO.class));
            }

            menuVO.setChildMenus(menuVOList);
        }
        return menuVOS;
    }

    public List<MenuVO> selectMenuVOByRoleIds(List<Integer> roleIds) {
        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdIn(roleIds);
        List<RoleMenu> roleMenus = roleMenuMapper.selectByExample(example);

        List<Integer> menuIds = roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());

        MenuExample menuExample = new MenuExample();
        menuExample.setOrderByClause("create_time");
        menuExample.setDistinct(true);
        menuExample.createCriteria().andIdIn(menuIds).andParentIdEqualTo(0);
        List<Menu> menus = menuMapper.selectByExample(menuExample);

        List<MenuVO> menuVOS = JsonUtils.jsonToList(JsonUtils.objectToJson(menus), MenuVO.class);
        for (MenuVO menuVO : menuVOS) {
            menuExample.clear();
            menuExample.setOrderByClause("create_time");
            menuExample.createCriteria().andParentIdEqualTo(menuVO.getId()).andIdIn(menuIds);
            List<Menu> childMenus = menuMapper.selectByExample(menuExample);
            //menuVO.setChildMenus(JsonUtils.jsonToList(JsonUtils.objectToJson(childMenus), MenuVO.class));
            List<MenuVO> menuVOList = JsonUtils.jsonToList(JsonUtils.objectToJson(childMenus), MenuVO.class);
            for (MenuVO vo : menuVOList) {
                menuExample.clear();
                menuExample.setOrderByClause("create_time");
                menuExample.createCriteria().andParentIdEqualTo(vo.getId()).andIdIn(menuIds);
                List<Menu> childMenuList = menuMapper.selectByExample(menuExample);
                vo.setChildMenus(JsonUtils.jsonToList(JsonUtils.objectToJson(childMenuList), MenuVO.class));
            }

            menuVO.setChildMenus(menuVOList);
        }
        return menuVOS;
    }

    public BaseResult menuIdList(Integer id) {
        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(id);
        List<RoleMenu> roleMenus = roleMenuMapper.selectByExample(example);
        List<Integer> collect = roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        return BaseResult.ok(collect);
    }
}
